#include "root.h"
extern UCHAR metaddr[7];
extern int write_param(dataop_t * op,UCHAR flag);
int proc_dataop(dpitf_t *pitf, dataop_pf pfunc, UCHAR *pallok)
{
	dataop_t op;
	UCHAR *prbuf, *psbuf;
	int rtn, slen;
	UCHAR bfail = 0;			
	UCHAR cnt_op = 0;
//	UCHAR infpg;
	UCHAR infps =0;
	*pallok = 1;
	pitf->slen = 0;
	slen = 0;

	if(pitf->rlen < LEN_DATAUNIT) return DPRTN_FAIL;
	
	prbuf = pitf->rbuf;
	op.rlen = pitf->rlen;
	psbuf = pitf->sbuf;
	op.smaxlen = pitf->smaxlen;
	op.type = pitf->type;
	op.addr = pitf->addr;
//	while(op.rlen >= LEN_DATAUNIT)
	{
		if(op.smaxlen <= LEN_DATAUNIT) return DPRTN_FAIL;

		cnt_op++;
		if(2 == cnt_op) *pallok = 0;
		if(memcmp(metaddr,prbuf,7)){
			infps = findmeter_metpid(prbuf);
			if(infps >= METER_NUM_MAX)return DPRTN_ERR;
			infps +=1;
			
		}else infps = 0;
		memcpy(psbuf, prbuf, LEN_DATAUNIT);
		psbuf += LEN_DATAUNIT;
		op.smaxlen -= LEN_DATAUNIT;
		slen += LEN_DATAUNIT;

		prbuf += LEN_INFPS;
		op.cid = *prbuf++;
		op.grpid = *prbuf++;
		op.rlen -= LEN_DATAUNIT;
		bfail = 0;

		op.rbuf = prbuf;
		op.sbuf = psbuf;
		op.infpid = infps;
		rtn = (*pfunc)(&op,op.type);
		if(DPRTN_FAIL == rtn) return DPRTN_FAIL;
		if(pitf->flag == DPFLAG_SET)
		{
			if(rtn) 
			{
				*pallok = 0;
				bfail = rtn;
			}
			prbuf += op.rskip;
			if(op.rlen < op.rskip) return DPRTN_FAIL;
			op.rlen -= op.rskip;
//			print_logo(0,"rlen%d\r\n",op.rlen);
//			os_sleep(10);
		}
		else
		{
			if(op.rlen < op.rskip) return DPRTN_FAIL;

			psbuf += op.sskip;
			if(op.smaxlen < op.sskip) return DPRTN_FAIL;
			op.smaxlen -= op.sskip;
			slen += op.sskip;
		}
			

		if(pitf->flag == DPFLAG_SET)
		{
			if(op.smaxlen < 1) return DPRTN_FAIL;
			*psbuf++ = bfail;
			op.smaxlen--;
			slen += 1;
		}
		else
		{
			prbuf += op.rskip;
			if(op.rlen < op.rskip) return DPRTN_FAIL;
			op.rlen -= op.rskip;
		}
	}

	pitf->slen = slen;
	return DPRTN_OK;
}
